/**
 * Created by jojo_yi on 2016/9/21.
 */
$(window).on("resize",function(){
    responsive();
});
$(function(){
    (function(p,l,o,w,i,n,g){if(!p[i]){p.GlobalSnowplowNamespace=p.GlobalSnowplowNamespace||[];
        p.GlobalSnowplowNamespace.push(i);p[i]=function(){(p[i].q=p[i].q||[]).push(arguments)
        };p[i].q=p[i].q||[];n=l.createElement(o);g=l.getElementsByTagName(o)[0];n.async=1;
        n.src=w;g.parentNode.insertBefore(n,g)}}(window,document,"script","/js/sp3.js","tkr"));

    window.tkr('newTracker', 'cf', 'perigee.wochacha.com', { // Initialise a tracker
        post: true,
        userFingerprint: false,
        cookieName: "_sp_",
        forceSecureTracker: true,
        platform:"xybc"
    });
    //window.tkr('enableActivityTracking', 2, 2);
    window.tkr('enableLinkClickTracking', null, true); // UE事件跟踪跳转连接，不包含类"barred"
    window.tkr('trackPageView'); // Track the page view with custom title
    window.tkr(function () {
        //console.log(this.cf.getUserFingerprint());
    });
});
$(function(){
    //page responsive
    responsive();
    // 下拉刷新
    $(".page").dropload({
        scrollArea : window,
        domUp : {
            domClass   : 'dropload-up',
            domRefresh : '<div class="dropload-refresh">↓下拉刷新</div>',
            domUpdate  : '<div class="dropload-update">↑释放刷新</div>',
            domLoad    : '<div class="dropload-load"><span class="loading"></span>重新加载中...</div>'
        },
        autoLoad: false,
        loadUpFn : function(me){
            setTimeout(function(){
                location.reload();
            },300);
            me.resetload();
        },
        threshold : 50
    });
    //箭头动画
    setTimeout(function(){
        $(".prizefly").addClass("prizeflyTransition");
    },0);
    //实物点击
    $("#my_matter").on("touchend",function(){
        if(webViewParam.urid==""||webViewParam.urid==null||webViewParam.urid==undefined){
            if (+(webViewParam["v"]) >= 8.5) {
                window.location.href='wccia://login/loginback';
            }else {
                alertAction("版本过低，请移步至个人中心登录注册。");
            }
        }else {
            var url=window.location.origin+"/pointmall/orderlist"+webParam+"&page=1&ordertype=20&laiyuan=2&hdtag=1";
            if(+webViewParam.v>=8.5) {
                window.location.href = "wccia://webview/" + url;
            }else{
                window.location.href= url;
            }
        }
    });
    //我的抵用券
    $("#my_ticket").on("touchend",function(){
        if(webViewParam.urid==""||webViewParam.urid==null||webViewParam.urid==undefined){
            if (+(webViewParam["v"]) >= 8.5) {
                window.tkr('trackStructEvent', 'xybc', 'click','', 'ticket_login', '');
                window.location.href='wccia://login/loginback';
            }else {
                alertAction("版本过低，请移步至个人中心登录注册。");
            }
        }else {
            window.tkr('trackStructEvent', 'xybc', 'click','', 'ticketBtn', '');
            var url="https://wap.koudaitong.com/v2/showcase/promocode/fetch?alias=dkvypgyz";
            if(+webViewParam.v>=8.5) {
                window.location.href = "wccia://webview/" + url;
            }else{
                window.location.href= url;
            }
        }
    });
    //animate
    var dialogAnimate = {   // 弹层相关动画
        start : function(){
            $(".dialog").show();
            $(".dialogBox").addClass("fadeInDownBig");
            setTimeout(function(){
                $(".redPacket").addClass("tada");
            },300);
        },
        end : function(){
            $(".redPacket").removeClass("tada");
            $(".dialogBox").removeClass("fadeInDownBig").addClass("fadeOutDown");
            setTimeout(function(){
                $(".dialogBox").removeClass("fadeOutDown");
                $(".dialog").hide().html("");
            },300)
        }
    };
    var dialo={
        alert:function(type,data,img){//结果
            var html='<div class="vertical-middle redPacket dialogBox">';
            html+='<div class="wrap">';
            if(type=="2"||type=="3"){
                html+='<div class="prizeImg">';
                html+='<img src="'+img+'">';
                html+='</div>';
            }else {
                html+='<div class="innate"></div>';<!-- 没有抽到 -->
            }
            html+='<div class="inner">';
            if(type=="1"){//没中
                html+='<h1>差一点就中了</h1>';
                html+='<p>换个姿势，再试一次！<p>';
            }else if(type=="2"){//优惠券
                html+='<h1>获得80元抵用券！</h1>';
                html+=data.islogin=="1"?'<p>您可以在积分好礼-兑换记录中查看并使用抵用券！':'<p>登录后，您可以在积分好礼-兑换记录中查看并使用抵用券！<p>';
            }else if(type=="3"){//实物
                html+='<h1>获得JUICY TANGO正装礼包！</h1>';
                html+='<p>填写收货地址后，我们将免费快递给您，您也可以在个人中心完善收货信息<p>';
            }else if(type=="150"){
                html+='<h1>次数用完啦！</h1>';
                html+='<p>不要贪心哦~<p>';
            }else {
                html+='<p>'+data+'<p>';
            }
            html+='</div>';
            html+='<div class="action">';
            if(type=="2"){
                html+='<button class="btn closeBtn">关闭</button>';<!-- 关闭 -->
                if(data.islogin=="1"){
                    html+='<button class="btn org_btn useBtn">去使用</button>';<!-- 登录 -->
                }else {
                    html+='<button class="btn org_btn loginBtn">去登录</button>';<!-- 登录 -->
                }
            }else if(type=="3"){
                html+='<button class="btn closeBtn">关闭</button>';<!-- 关闭 -->
                html+='<button class="btn org_btn addressBtn">去完善信息</button>';<!-- 收货地址 -->
            }else {
                html+='<button class="btn commonBtn">确定</button>';<!-- 确定 -->
            }
            html+='</div>';
            html+='</div>';
            html+='</div>';
            $(".dialog").html(html);
        }
    };
    //抽奖
    $("#jsOpenBtn").on("tap",function(){
        $(".dialog").show();
        if($(this).data("ajax")!="0"){
            return false;
        }
        if(todayCanPlay=="0"){
            dialo.alert("150");
        }
        window.tkr('trackStructEvent', 'xybc', 'click','', 'xybcbtn', '');
        $(this).data("ajax","1");
        $.get("/activity/xybcdo",webViewParam, function(data, status) {
            if(data==""||data==null||data==undefined){
                dialo.alert("99","网络异常，请稍候重试");
            }else {
                data=JSON.parse(data);
                if(data.errno=="0"){
                    if(data.type=="1"){//没中奖
                        dialo.alert("1",data);
                    }else if(data.type=="2"){//优惠券
                        dialo.alert("2",data,data.img);
                    }else if(data.type=="3"){//实物
                        dialo.alert("3",data,data.img);
                    }else {
                        dialo.alert("150");
                    }
                    todayCanPlay=data.todayFreeTimes;
                    dialogAnimate.start();
                }else if(data.errno=="150"){//没有次数
                    dialo.alert("150");
                    todayCanPlay="0";
                    dialogAnimate.start();
                }else {
                    if(data.msg==""||data.msg==undefined||data.msg==null){
                        dialo.alert("99","网络异常，请稍候重试");
                    }else {
                        dialo.alert("99",data.msg);
                    }
                }
            }
            $("#jsOpenBtn").data("ajax","0");
        });
    });
    var dialog=$(".dialog");
    //确定
    dialog.on("tap",".commonBtn,.closeBtn",function(){
        dialogAnimate.end();
    });
    //使用优惠券
    dialog.on("tap",".useBtn",function(){
        window.tkr('trackStructEvent', 'xybc', 'click', 'xybc', 'useBtn','');
        var url="https://wap.koudaitong.com/v2/showcase/promocode/fetch?alias=dkvypgyz";
        window.location.href = url;
    });
    //版本判断登录及完善收货地址
    if (+(webViewParam["v"]) >= 8.5) {
        // 登录协议
        dialog.on("tap",".loginBtn",function(){
            window.tkr('trackStructEvent', 'xybc', 'click', 'xybc', 'loginBtn','');
            window.location.href='wccia://login/loginback';
        });
        //收货地址协议
        dialog.on("tap",".addressBtn",function(){
            window.tkr('trackStructEvent', 'xybc', 'click', 'xybc', 'addressBtn','');
            window.location.href = "wccia://jump/65";
        });
    }else{
        // 登录提示弹框
        dialog.on("tap",".loginBtn",function(){
            alertAction("版本过低，请移步至个人中心登录注册。");
        });
        //收货地址弹窗
        dialog.on("tap",".addressBtn",function(){
            alertAction("版本过低，请前往个人中心设置默认地址为收货地址。");
        });
    }
});

function responsive(){
    var width = $('body').width();
    $('html').css('font-size',width/10+'px');
}
//回调函数接受客户端传过来的urid，并且重新拼接新的路由，并且重新跳转。
function loginback(gurid) {
    $.get("/activity/checkregister",
        {"urid":gurid,"udid":webViewParam["udid"],"v":webViewParam["v"],"ori_v":webViewParam["ori_v"],"lat":webViewParam["lat"],"lng":webViewParam["lng"],"connectnet":webViewParam["connectnet"],"city_id":webViewParam["city_id"],"os":webViewParam["os"],"barcode":webViewParam["barcode"]},
        function(data, status) {});
    var newUrl = '/activity/xybc?urid='+gurid+'&udid='+webViewParam["udid"]+'&v='+webViewParam["v"]+'&ori_v='+webViewParam["ori_v"]+'&lat='+webViewParam["lat"]+'&lng='+webViewParam["lng"]+'&connectnet='+webViewParam["connectnet"]+'&city_id='+webViewParam["city_id"]+'&os='+webViewParam["os"]+'&barcode='+webViewParam["barcode"];
    location.replace(newUrl);
}
//弹窗
function alertAction(text,callback){
    if (!callback) {callback = function(){}}
    var alertTemplate = '<div class="alert-mask" ontouchmove="return false"><div class="alert-box"><p>'+text+'</p><div class="action"><div class="btn right close">确定</div></div></div></div>';
    $("body").append(alertTemplate);
    $(".alert-box .close").click(function(){$(".alert-mask").remove();callback()});
}



